package com.taxi.driver.module.recruit.selectcity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qianxx.view.HeadView;
import com.taxi.driver.R;
import com.taxi.driver.common.Application;
import com.taxi.driver.common.BaseFragment;
import com.taxi.driver.data.entity.ResourcesEntity;
import com.taxi.driver.module.recruit.CarInfoEntryActivity;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.yokeyword.indexablerv.IndexableLayout;
import me.yokeyword.indexablerv.SimpleFooterAdapter;

/**
 * Created by Curry on 2018/5/14.
 */

public class SelectCityFragment extends BaseFragment implements SelectCityContract.View
{
    @BindView(R.id.indexable_layout)
    IndexableLayout indexableLayout;
    @BindView(R.id.head_view)
    HeadView headView;
//    @BindView(R.id.ll_city)
//    LinearLayout llCity;
    private List<ResourcesEntity> mCitys;
    private SimpleFooterAdapter<ResourcesEntity> mFooterAdapter;
    @Inject
    SelectCityPresenter mPresenter;


    SelectCityAdapter mCityAdapter;
    public static SelectCityFragment newInstance() {
        Bundle args = new Bundle();
//        args.putSerializable(KEY_ADDRESS_TYPE, type);
//        args.putSerializable(KEY_CAR_TYPE, carType);
        SelectCityFragment fragment = new SelectCityFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        mView = inflater.inflate(R.layout.fragment_select_city, container, false);
        ButterKnife.bind(this, mView);

        initView();
        initListener();
        return mView;
    }
    private void initView()
    {   headView.setTitle("选择城市");
        indexableLayout.setLayoutManager(new LinearLayoutManager(getContext()));
        mCityAdapter = new SelectCityAdapter(getContext());
        indexableLayout.setAdapter(mCityAdapter);
        indexableLayout.setCompareMode(IndexableLayout.MODE_FAST);
        mPresenter.getAllCity();
    }
    @Override
    public void onResume() {
        super.onResume();
        mPresenter.subscribe();
    }
//    @OnTextChanged(value = R.id.et_city)
//    public void onEditTextChanged(CharSequence s) {
//        filterData(s.toString());
//        if (TextUtils.isEmpty(s)) {
//            DisplayUtil.hideKeyboard(etCity);
//        }
//    }
    @Override
    public void onPause() {
        super.onPause();
        mPresenter.unsubscribe();
    }
    private void initListener()
    {
        mCityAdapter.setOnItemContentClickListener((v, originalPosition, currentPosition, entity) -> {
            CarInfoEntryActivity.startIntent(getActivity(),entity.getName());
//            if (getString(R.string.other_city_open).equals(entity.getName())) {
//                return;
//            }
//            if (addressType == AddressType.DEST_SEND) {
//                SelectAirportActivity.startIntent(getContext(), mCarType, entity.getName());
//            } else {
//                SelectAddressActivity.startIntent(getContext(), addressType, mCarType, entity.getName());
//            }
            getActivity().finish();
        });
    }


    @Override
    public void showAllCitys(List<ResourcesEntity> entities)
    {
        mCitys = entities;
        mCityAdapter.setDatas(entities);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        DaggerSelectCityComponent.builder()
                .appComponent(Application.getAppComponent())
                .selectCityModule(new SelectCityModule(this))
                .build().inject(this);
    }

}
